"use client"
import { useParams, useRouter } from 'next/navigation';
import React, { useState, useEffect } from 'react';
import type { RadioChangeEvent, DatePickerProps } from 'antd';
import { FormProps, Rate, Button, Radio, Form, Input, DatePicker, InputNumber } from 'antd';
import { getdata } from '@/app/request/http';
import * as api from '../../request/http';

type FieldType = {
  username?: string;
  password?: string;
  remember?: string;
};
const style: React.CSSProperties = {
  display: 'flex',
  flexDirection: 'column',
  gap: 8,
};

const AddData = (props) => {
  console.log(props);

  let [getdatas, setGetdata] = useState([])

  function update() {
    getdata().then(res => {
      return setGetdata(res);
    })
  }

  useEffect(() => {
    update()
  }, [])
  let id = location.href.split("=")[1];

  let newdata = getdatas.filter(item => {
    return item.id == id
  })
  console.log(newdata);
  const [value, setValue] = useState(1);

  let [form] = Form.useForm()
  useEffect(() => {
    form.setFieldsValue(props.record)
  })
  let { obj } = useParams()
  console.log(obj);



  const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
    if (props.title == "添加") {
      let obj = {
        ...values,
        flag: false
      }
      api.http_addtable(obj)
    } else {
      let obj = {
        ...props.data,
        ...values
      }
      api.http_edittable(obj)
    }
    location.reload()
    props.onOk()

  };



  return (
    <Form
      form={form}
      onFinish={onFinish}
    >
      <Form.Item<FieldType>
        label="客户名称"
        name="name"
        rules={[{ required: true, message: '请输入客户名称!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item<FieldType>
        label="客户状态"
        name="flag"
      >
        <Radio.Group
          style={style}
          value={value}
          options={[
            { value: 0, label: '跟进' },
            { value: 1, label: '已经跟进' },
          ]}
        />
      </Form.Item>

      <Form.Item<FieldType>
        label="客户星级"
        name="address"
      >
        <Rate />
      </Form.Item>

      <Form.Item<FieldType>
        label="首联系人"
        name="topOne"
        rules={[{ required: true, message: '请输入首联系人!' }]}
      >
        <Input />
      </Form.Item>
      <Form.Item<FieldType>
        label="手机号码"
        name="call"
        rules={[{ required: true, message: '请输入手机号码!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item<FieldType>
        label="手机号码"
        name="time"
        rules={[{ required: true, message: '请输入手机号码!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item<FieldType>
        label="未跟进天数"
        name="day"
        rules={[{ required: true, message: '请输入手机号码!' }]}
      >
        <Form.Item>
          <InputNumber />
        </Form.Item>


      </Form.Item>
      <Form.Item label={null}>
        <Button type="primary" htmlType="submit">
          确定
        </Button>
      </Form.Item>
    </Form>
  )
};

export default AddData;